<template>
  <div class="page goods-index">
    <div class="scroll-view">
      <customHeader middleTitle="商品管理" />
      <div class="page-body" v-if="!loadding&&authDashBoard">
        <template>
          <noData v-if="!opened" msg="很抱歉，您还没有订购商家小程序，订购后才能发布商品。请联系您的销售经理，订购商家小程序"></noData>
        </template>
        <template v-if="opened&&!created">
          <noData msg="您已订购小程序，可以发布商品到小程序售卖。"></noData>
          <div class="btn" @click="linkToAdd">发布商品</div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue';
import customHeader from '../../components/header/header';
import noData from "../../components/noData/noData";
import { Toast,MessageBox } from 'mint-ui';
import jsBridge from "../../tools/jsBridge.js";
import jsAuthority from "../../tools/jsAuthority.js";
export default {
  name: 'goodsIndex',
  data() {
    return {
      opened: false,
      created: false,
      loadding: false,
      authAddOrEdit: false,
      authDashBoard: false,
    };
  },
  methods: {
    linkToAdd() {
      if (!this.authAddOrEdit) {
        Toast("暂无权限");
        return;
      }
      this.$router.push({ path: '/goodsDataBoard/goodsManage/edit', query: { close: "true" } });
    },
    hasGoods() {
      this.loadding = true;
      const url = "/newapp/goods/service/isCreatedGoods.do";
      this.ajax({
        url: url,
        type: "get",
        controlLoading: true
      })
        .then(res => {
          this.loadding = false;
          let created = false;
          if (res.success) {
            if (res.resultObject) {
              created = true;
            } else {

            }
          } else {
            this.noMore = true;
            Toast(res.msg);
          }
          this.created = created;
          if (created) {
            this.$router.push({ path: '/goodsDataBoard', query: { close: "true" } });
          }
        })
        .catch((err) => {

        });
    },
    //是否有门店订购过小程序套餐
    isOpenMp() {
      this.loadding = true;
      const url = "/newapp/shop/getShopSelectForMarket.do";
      this.ajax({
        url: url,
        type: "get",
        controlLoading: true
      })
        .then(res => {
          let has = false;
          this.loadding = false;
          if (res.success) {
            const data = res.resultObject;
            data.pageData.map((city) => {
              if (city && city['shops'] && city.shops.length) {
                has = true;
              }
            })
            if (has) {
              this.hasGoods();
            }

          } else {
            Toast(res.msg);
          }
          this.opened = has;
        })
        .catch((err) => {

        });
    },
    getAuth() {
      jsAuthority.checkedSuccess(() => {
        this.authAddOrEdit = jsAuthority.checked(
          "app.goodsManage.addOrEdit"
        );
      });
      jsAuthority.checkedSuccess(() => {
        this.authDashBoard = jsAuthority.checked(
          "app.goodsManage.dashBoard"
        );
        if (!this.authDashBoard) {
          MessageBox.alert("暂无权限").then(action => {
            jsBridge.closeWindow();
          });
        } else {
          this.isOpenMp();
        }
      });
    }
  },
  components: {
    customHeader,
    noData,
  },
  created() {
    this.getAuth();
  },
};
</script>
<style lang='scss' scoped>
@import "goodsManage";
</style>
